var objMeeboHistoryTimer;
var objMeeboInit;
var objMeeboLogin;
var intMeeboRev = 0;
var objEvents = new Array();

var objMeeboStatusTimer;
var objMeeboEventTimer;
var objMeeboDBGTimer;
var loggedin = false;
var curVersion = "0.2";

function history_reload() {
    var backDOM = chrome.extension.getBackgroundPage().document.getElementById("conversations");
    var popDOM = document.getElementById("conversations");
    var arrHistories = backDOM.getElementsByTagName("div");
    for(var i = 0; i < arrHistories.length; i++) {
        if(arrHistories[i].id.indexOf("History_") != -1) {
            if( document.getElementById(arrHistories[i].id).innerHTML != arrHistories[i].innerHTML) {
                document.getElementById(arrHistories[i].id).innerHTML = arrHistories[i].innerHTML;
                document.getElementById(arrHistories[i].id).scrollTop = document.getElementById(arrHistories[i].id).scrollHeight;
            }
        }
    }
}
function switchIt(obj) {
    if(obj.parentNode.lastChild.style.display != 'block') {
        for(var i = 0; i < obj.parentNode.parentNode.childNodes.length; i++) {
            if(obj.parentNode.parentNode.childNodes[i].lastChild instanceof HTMLDivElement)
                $(obj.parentNode.parentNode.childNodes[i].lastChild).hide('fast');
        }
        if(obj.parentNode.className == "buddy_unread") {
            chrome.extension.sendRequest({
                action: "resetUnread",
                uin: obj.parentNode.id
                }, function(response) {});
            obj.parentNode.className = "buddy";
        }
        $(obj.parentNode.lastChild).show('fast');
    } else {
        $(obj.parentNode.lastChild).hide('fast');
    }
    obj.parentNode.lastChild.firstChild.scrollTop = obj.parentNode.lastChild.firstChild.scrollHeight;
}
function switchItID(objID) {
    var obj = document.getElementById(objID);
    var cont = document.getElementById("conversations");
    for(var i = 0; i < cont.childNodes.length; i++) {
        if(cont.childNodes[i].lastChild instanceof HTMLDivElement) {
            cont.childNodes[i].lastChild.style.display = "none";
        }
    }
    $(obj.lastChild).show('fast');
    obj.lastChild.firstChild.scrollTop = obj.lastChild.firstChild.scrollHeight;
}
function hideIt(obj) {
    $(obj.parentNode.parentNode).hide('fast');
    chrome.extension.sendRequest({
        action: "closeConversation",
        convid: obj.parentNode.parentNode.id
        }, function(response) {});
}
function addText(obj) {
    if (event.keyCode == 13) {
        //obj.parentNode.parentNode.firstChild.innerHTML = obj.parentNode.parentNode.firstChild.innerHTML+'<span style=\"color:red;\">You: </span><span>'+obj.value+'</span><br/>';
        obj.parentNode.parentNode.firstChild.scrollTop = obj.parentNode.parentNode.firstChild.scrollHeight;
        var strname = obj.parentNode.parentNode.parentNode.id.substring(obj.parentNode.parentNode.parentNode.id.indexOf("_")+1);
        chrome.extension.sendRequest({
            action: "sendMessage",
            user: strname,
            name: obj.parentNode.parentNode.parentNode.name,
            message: obj.value
            }, function(response) {});
        obj.value = "";
    }
}
function changeStatus(obj, strMessage){
    document.getElementById("status").innerText = obj.innerText;
    chrome.browserAction.setIcon({
        path: "../images/status_"+obj.id+".png"
        });
    chrome.extension.sendRequest({
        action: "changeStatus",
        status:  obj.id,
        message: strMessage,
        text:obj.innerText
        }, function(response) {});
}
function switchBuddyStatus(obj) {
    for(var i = 0; i < document.getElementById("switchContainer").childNodes.length; i++) {
        if(document.getElementById("switchContainer").childNodes[i] instanceof HTMLDivElement)
            $(document.getElementById("switchContainer").childNodes[i]).hide("fast");
    }
    $("#"+obj).show("fast");
}
function searchBuddy(obj) {
    var key = obj.value;
    if(key != "") {
        for(var i = 0; i < document.getElementById("buddylistcontainer").childNodes.length; i++) {
            if(document.getElementById("buddylistcontainer").childNodes[i] instanceof HTMLDivElement)
                if(document.getElementById("buddylistcontainer").childNodes[i].innerText.toLowerCase().indexOf(key.toLowerCase()) != -1) {
                    document.getElementById("buddylistcontainer").scrollTop = document.getElementById(document.getElementById("buddylistcontainer").childNodes[i].id).offsetTop - 190;
                    document.getElementById(document.getElementById("buddylistcontainer").childNodes[i].id).className = "buddy_unread";
                } else {
                    document.getElementById(document.getElementById("buddylistcontainer").childNodes[i].id).className = "buddy";
                }
        }
    }
}
function createConversation(obj) {
    document.getElementById("Buddy_"+obj.parentNode.id).style.display = "block";
    document.getElementById("Buddy_"+obj.parentNode.id).lastChild.style.display = "none";
    switchItID("Buddy_"+obj.parentNode.id);

}
function onLoadPopup() {
    var backDOM = chrome.extension.getBackgroundPage();
    chrome.browserAction.setBadgeText({
        text: ""
    });
    document.getElementById("container").innerHTML = backDOM.document.getElementById("container").innerHTML;
    if(document.getElementById("error").innerText == "") {
        document.getElementById("header").style.display = "block";
        document.getElementById("error").style.display = "none";
    } else {
        document.getElementById("header").style.display = "none";
        document.getElementById("error").style.display = "block";
    }
    objMeeboHistoryTimer = window.setInterval("history_reload()", 1000);
}
function openOptions() {
    chrome.tabs.create({
        'url' : chrome.extension.getURL('pages/options.html')
        }, null);
}
function openManual() {
    chrome.tabs.create({
        'url' : chrome.extension.getURL('pages/manual.html')
        }, null);
}


function tryAgain() {
    chrome.extension.sendRequest({
        action: "Login"
    }, function(response) {});
}
function onBackgroundLoad() {
    if(localStorage["lastVersion"] != curVersion) {
        openManual();
        //localStorage["lastVersion"] = curVersion;
    }
    if(localStorage.length == 0)
        chrome.tabs.create({
            "url" : chrome.extension.getURL("pages/options.html")
            }, null);

    if(localStorage["password"] && localStorage["login"]) {
        if(localStorage["autologin"] == "true") {
            meeboInit();
        } else
            document.getElementById("error").innerHTML += "<a onclick=\"tryAgain();\" href=\"#\">Sign in!</a>";
    } else {
        document.getElementById("error").innerHTML += "<br><a onclick=\"openOptions();\" href=\"#\">Please edit your login details</a></div>";
    }
}
function meeboInit() {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.meebo.com/mcmd/start?type=mobile&bcookie=&ts="+ts.getTime(), true);
    xhr.send();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            objMeeboInit = JSON.parse(xhr.responseText);
            meeboLogin();
        }
    }
}
function meeboLogin() {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    var params = "sessionKey="+objMeeboInit.sessionKey+"&clientId="+objMeeboInit.clientId+"&gmt=60&ssl=true&num=1&username0="+localStorage["login"]+"&protocol0="+localStorage["network"]+"&state0=available&message0=&isauthtoken0=false&network0=icq&ispartnerlogin0=false&password0="+localStorage["password"];
    xhr.open("POST", "https://www.meebo.com/mcmd/joinexisting", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(params);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            objMeeboLogin = JSON.parse(xhr.responseText);
            objMeeboStatusTimer = window.setInterval("meeboStatus()", 45000);
            meeboEventInit();
        }
    }
}
function meeboStatus() {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.meebo.com/mcmd/test?sessionKey="+objMeeboInit.sessionKey+"&clientId="+objMeeboInit.clientId+"&ts="+ts.getTime(), true);
    xhr.send();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if("\"success\"" != xhr.responseText)
                onBackgroundLoad();
        }
    }
}
function meeboEventInit() {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.meebo.com/mcmd/events?sessionKey="+objMeeboInit.sessionKey+"&rev="+intMeeboRev+"&clientId="+objMeeboInit.clientId, true);
    xhr.send();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            objEvents[intMeeboRev] = JSON.parse(xhr.responseText);
            intMeeboRev++;
            meeboParseJSON(JSON.parse(xhr.responseText));
            objMeeboEventTimer = window.setInterval("meeboEvent()", 10000);
            meeboDBG();
        }
    }
}
function meeboEvent() {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.meebo.com/mcmd/events?sessionKey="+objMeeboInit.sessionKey+"&rev="+intMeeboRev+"&clientId="+objMeeboInit.clientId, true);
    xhr.send();
    intMeeboRev++;
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if(intMeeboRev <= 4)
                objEvents[intMeeboRev] = JSON.parse(xhr.responseText);
            meeboParseJSON(JSON.parse(xhr.responseText));
        }
    }
}
function meeboDBG() {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.meebo.com/mcmd/dbg?sessionKey="+objMeeboInit.sessionKey+"&cat=javascript&m=meebo-demographic%3Dage%3Aundefined%2Cgender%3Aundefined&clientId="+objMeeboInit.clientId, true);
    xhr.send();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {}
    }
}
function meeboLogout() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.meebo.com/mcmd/detach", true);
    xhr.send();
    xhr.onreadystatechange = function() {}
}
function meeboSendMessage(strToUIN, strMessage, strProtocol) {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    var params = "sessionKey="+objMeeboInit.sessionKey+"&sender="+localStorage["login"]+"&receiver="+strToUIN+"&protocol="+strProtocol+"&msg="+strMessage+"&clientId="+objMeeboInit.clientId;

    xhr.open("POST", "https://www.meebo.com/mcmd/send", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(params);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {}
    }
}
function meeboSetStatus(strState, strMessage) {
    var ts = new Date();
    var xhr = new XMLHttpRequest();
    var params = "sessionKey="+objMeeboInit.sessionKey+"&num=1&1user="+localStorage["login"]+"&1protocol="+localStorage["network"]+"&1state="+strState+"&1message="+strMessage+"&clientId="+objMeeboInit.clientId;

    xhr.open("POST", "https://www.meebo.com/mcmd/setstatus", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(params);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {}
    }
}
function meeboParseJSON(objJSON) {
    for (var key in objJSON["events"]) {
        if(loggedin == false) {
            chrome.browserAction.setIcon({
                path: "../images/status_online.png"
            });
            loggedin = true;
        }
        switch (objJSON["events"][key].type) {
            case "buddy::away":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_away.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_away.png";
                }else{
                    document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><div id=\"Protocol_"+objJSON["events"][key].data.buddy+"\" style=\"display:none\">"+objJSON["events"][key].data.protocol+"</div><img class=\"img\" id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_away.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                    document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img class=\"img\" id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_away.jpg\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                }
                break;
            case "buddy::busy":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_busy.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_busy.png";
                }else{
                    document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><div id=\"Protocol_"+objJSON["events"][key].data.buddy+"\" style=\"display:none\">"+objJSON["events"][key].data.protocol+"</div><img id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" class=\"img\" src=\"../images/status_busy.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                    document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img class=\"img\" id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_busy.jpg\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                }
                break;

            case "buddy::online":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                }else{
                    document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><div id=\"Protocol_"+objJSON["events"][key].data.buddy+"\" style=\"display:none\">"+objJSON["events"][key].data.protocol+"</div><img id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" class=\"img\" src=\"../images/status_online.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                    document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img class=\"img\" id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_online.png\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                }
                break;
            case "buddy::backa":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                }else{
                    document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><div id=\"Protocol_"+objJSON["events"][key].data.buddy+"\" style=\"display:none\">"+objJSON["events"][key].data.protocol+"</div><img id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" class=\"img\" src=\"../images/status_online.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                    document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img class=\"img\" id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_online.png\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                }
                break;
            case "buddy::backb":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                }else{
                    document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><div id=\"Protocol_"+objJSON["events"][key].data.buddy+"\" style=\"display:none\">"+objJSON["events"][key].data.protocol+"</div><img id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" class=\"img\" src=\"../images/status_online.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                    document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img class=\"img\" id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_online.png\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                }
                break;
            case "buddy::idle":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_online.png";
                }else{
                    document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><div id=\"Protocol_"+objJSON["events"][key].data.buddy+"\" style=\"display:none\">"+objJSON["events"][key].data.protocol+"</div><img id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" class=\"img\" src=\"../images/status_online.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                    document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img class=\"img\" id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_online.jpg\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                }
                break;
            case "buddy::offline":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_offline.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_offline.png";
                }else{
                    if(objJSON["events"][key].data.buddyalias) {
                        document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><img class=\"img\" id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_offline.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                        document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" class=\"img\" src=\"../images/status_offline.png\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                    }
                }
                break;
            case "buddy::login":
                if(document.getElementById("Buddy_"+objJSON["events"][key].data.buddy)){
                    if(document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Bud_"+objJSON["events"][key].data.buddy).src = "../images/status_offline.png";
                    if(document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy))
                        document.getElementById("Status_Con_"+objJSON["events"][key].data.buddy).src = "../images/status_offline.png";
                }else{
                    if(objJSON["events"][key].data.buddyalias) {
                        document.getElementById("conversations").innerHTML += "<div class=\"buddy\" style=\"display:none\" id=\"Buddy_"+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"switchIt(this)\" ><img class=\"img\" id=\"Status_Con_"+objJSON["events"][key].data.buddy+"\" src=\"../images/status_offline.png\" /><span id=\"alias_"+objJSON["events"][key].data.buddy+"\" class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span><span style=\"float:right;vertical-align:middle;width:10px;color:red;padding-top:3px;text-align:right;padding-right:10px;\" onclick=\"hideIt(this)\">x</span></div><div class=\"display\"><div id=\"History_"+objJSON["events"][key].data.buddy+"\" name=\""+objJSON["events"][key].data.buddyalias+"\" class=\"history\"></div><div class=\"message\"><input type=\"text\" onkeydown=\"addText(this)\" value=\"\" /></div></div></div>";
                        document.getElementById("buddylistcontainer").innerHTML += "<div class=\"buddy\" id=\""+objJSON["events"][key].data.buddy+"\"><div class=\"profile\" onclick=\"createConversation(this);switchBuddyStatus('conversations')\"><img id=\"Status_Bud_"+objJSON["events"][key].data.buddy+"\" class=\"img\" src=\"../images/status_offline.png\" /><span class=\"name\">"+objJSON["events"][key].data.buddyalias+"</span></div></div>";
                    }
                }
                break;
            case "user::offline":
                chrome.browserAction.setIcon({
                    path: "../images/status_offline.png"
                });
                loggedin = false;
                window.clearInterval(objMeeboStatusTimer);
                window.clearInterval(objMeeboEventTimer);
                window.clearInterval(objMeeboDBGTimer);
                intMeeboRev = 0;
                document.getElementById("error").innerHTML = objJSON["events"][key].data.data.description;
                document.getElementById("error").innerHTML += "<br><a onclick=\"openOptions();\" href=\"#\">Please edit your login details</a>";
                document.getElementById("error").innerHTML += "<br><a onclick=\"tryAgain();\" href=\"#\">Sign in again!</a>";
                document.getElementById("error").style.display = "block";
                var notification = webkitNotifications.createNotification(chrome.extension.getURL("images/icon_mess.png"), "Meebo Notifier", "Login failed! "+objJSON["events"][key].data.data.description);
                notification.show();
                setTimeout(function(){
                    notification.cancel();
                }, 3000);
                break;
            case "user::online":
                chrome.browserAction.setIcon({
                    path: "../images/status_online.png"
                });
                loggedin = true;
                document.getElementById("error").innerText = "";
                document.getElementById("error").style.display = "none";
                var notification = webkitNotifications.createNotification(chrome.extension.getURL("images/icon_mess.png"), "Meebo Notifier", "Login successfull!");
                notification.show();
                setTimeout(function(){
                    notification.cancel();
                }, 3000);
                break;
            case "im::received":
                document.getElementById("Buddy_"+objJSON["events"][key].data.sender).style.display = "block";
                document.getElementById("Buddy_"+objJSON["events"][key].data.sender).className = "buddy_unread";
                var date = new Date();

                document.getElementById("History_"+objJSON["events"][key].data.sender).innerHTML += "<div class=\"historymessage\"><span style=\"color: green\">"+date.toLocaleTimeString()+" - "+objJSON["events"][key].data.d.a+": </span><span>"+objJSON["events"][key].data.message+"</span></div>";
                var notification = webkitNotifications.createNotification(chrome.extension.getURL("images/icon_mess.png"), document.getElementById("alias_"+objJSON["events"][key].data.sender).innerText, objJSON["events"][key].data.message);
                notification.show();
                setTimeout(function(){
                    notification.cancel();
                }, 3000);
            //chrome.browserAction.setBadgeText({text: "New"});
            default: ;
        }
    }
}

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        if (request.action == "changeStatus") {
            document.getElementById("status").innerText = request.text;

            meeboSetStatus(request.status, request.message);
        }
        if (request.action == "sendMessage") {
            var ts = new Date();

            document.getElementById("History_"+request.user).innerHTML += "<div class=\"historymessage\"><span style=\"color: red\">"+ts.toLocaleTimeString()+" - You: </span><span>"+request.message+"</span></div>";
            document.getElementById("Buddy_"+request.user).style.display = "block";
            meeboSendMessage(request.user, request.message, document.getElementById("Protocol_"+request.user).innerText);
        }
        if(request.action == "resetUnread") {
            document.getElementById(request.uin).className = "buddy";
        }
        if(request.action == "closeConversation") {
            document.getElementById(request.convid).style.display = "none";
        }
        if(request.action == "Login") {
            window.clearInterval(objMeeboStatusTimer);
            window.clearInterval(objMeeboEventTimer);
            window.clearInterval(objMeeboDBGTimer);
            intMeeboRev = 0;
            meeboInit();
        }
    }
);